﻿using System;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SadlandVillage.Core;

namespace SadlandVillage.Minigames.Microgames
{
    public class MicroGames : SadlandVillage.Core.MiniGame
    {

        //BALLOON POP GAME
        private Sprite balloon;
        private Sprite balloon2;
        private Sprite balloon3;
        private Sprite balloon4;
        private Sprite balloon5;
        private Sprite balloon6;
        private Texture2D ballonBackground;
        private Sprite needle;

        private Color[] needleColor;
        private Color[] balloonColor;
        private Color[] balloon2Color;
        private Color[] balloon3Color;
        private Color[] balloon4Color;
        private Color[] balloon5Color;
        private Color[] balloon6Color;

        private bool[] popedBalloons;

        //BUG MICROGAME

        private Sprite roach;
        private Sprite deadRoach;
        private Sprite shoe;
        private Texture2D bugBackground;

        private Color[] roachColor;
        private Color[] shoeColor;

        private bool roachIsDead;
        private float roachRotation;
        private int lastDirection;
        private int direction;
        private bool isToChangeDirection;

        //JAR MICROGAME

        private Sprite lightJar;
        private Sprite middleHeavyJar;
        private Sprite heavyJar;
        private Sprite person;
        private Sprite hurtPerson;
        private Sprite hammer;
        private Texture2D jarBackground;

        private Color[] lightJarColor;
        private Color[] middleHeavyJarColor;
        private Color[] heavyJarColor;
        private Color[] personColor;
        private Color[] hammerColor;

        int numberOfColisions;
        bool isPersonHurt;

        //ALL MICROGAMES 
        private Texture2D chances;
        private Sprite chancesName;
        private bool wasPressed;
        private double timeSpent;
        // private SpriteFont font;

        private int ScreenWidth;
        private int ScreenHeight;

        private enum MicrogamesType
        {
            BALLOONPOP = 0, FRUITCUT = 1, NURSE = 2, BIKEWHEEL = 3,
            SAYTHEDIFERENT = 4, FLOWERJAR = 5, BUG = 6, PULLEY = 7,
            BYE = 8, SAYTHEWRONG = 9
        }

        private MicrogamesType micro;
        private int numberOfChances;

        public override event BackToMapEventHandler GameCancelled;
        //public virtual event BackToMapEventHandler GameWon;
        //public virtual event BackToMapEventHandler GameLost;

        public MicroGames(ContentManager content, SpriteBatch spriteBatch)
            : base(content, spriteBatch)
        {
            // content.RootDirectory = "Content"; // Essa linha comecou a dar pau, so deus sabe pq! Mas ela nao esta sendo necessaria
            this.numberOfChances = 3; //Quando for zero o jogo está perdido, e a cada microjog perdido deve ser decrementada em um.
            this.micro = MicrogamesType.BALLOONPOP;
            this.timeSpent = 0;
            this.ScreenHeight = spriteBatch.GraphicsDevice.Viewport.Height;
            this.ScreenWidth = spriteBatch.GraphicsDevice.Viewport.Width;

        }

        public override void LoadContent()
        {

            //BALLOON GAME
            ballonBackground = content.Load<Texture2D>(@"Images\balaoBack");
            balloon = new Sprite(content.Load<Texture2D>(@"Images\balao"), new Vector2(0, 200));
            balloon2 = new Sprite(content.Load<Texture2D>(@"Images\balaoLaranja"), new Vector2(100, 100));
            balloon3 = new Sprite(content.Load<Texture2D>(@"Images\balaoBranco"), new Vector2(200, 0));
            balloon4 = new Sprite(content.Load<Texture2D>(@"Images\balao"), new Vector2(500, 150));
            balloon5 = new Sprite(content.Load<Texture2D>(@"Images\balaoLaranja"), new Vector2(400, 300));
            balloon6 = new Sprite(content.Load<Texture2D>(@"Images\balaoBranco"), new Vector2(600, 100));
            needle = new Sprite(content.Load<Texture2D>(@"Images\needleBalao"), new Vector2(300, 400));

            //Guardando as cores das imagens em arrays para deteccção da colisão
            balloonColor = new Color[balloon.texture.Width * balloon.texture.Height];
            balloon2Color = new Color[balloon2.texture.Width * balloon2.texture.Height];
            balloon3Color = new Color[balloon3.texture.Width * balloon3.texture.Height];
            balloon4Color = new Color[balloon4.texture.Width * balloon4.texture.Height];
            balloon5Color = new Color[balloon5.texture.Width * balloon5.texture.Height];
            balloon6Color = new Color[balloon6.texture.Width * balloon6.texture.Height];
            needleColor = new Color[needle.texture.Width * needle.texture.Height];

            balloon.texture.GetData(balloonColor);
            balloon2.texture.GetData(balloon2Color);
            balloon3.texture.GetData(balloon3Color);
            balloon4.texture.GetData(balloon4Color);
            balloon5.texture.GetData(balloon5Color);
            balloon6.texture.GetData(balloon6Color);
            needle.texture.GetData(needleColor);

            // criando os bounding boxes

            this.needle.rectangle = new Rectangle((int)needle.position.X, (int)needle.position.Y, needle.texture.Width, needle.texture.Height);
            this.balloon.rectangle = new Rectangle((int)balloon.position.X, (int)balloon.position.Y, balloon.texture.Width - 20, balloon.texture.Height - 51);
            this.balloon2.rectangle = new Rectangle((int)balloon2.position.X, (int)balloon2.position.Y, balloon2.texture.Width - 20, balloon2.texture.Height - 51);
            this.balloon3.rectangle = new Rectangle((int)balloon3.position.X, (int)balloon3.position.Y, balloon3.texture.Width - 20, balloon3.texture.Height - 51);
            this.balloon4.rectangle = new Rectangle((int)balloon4.position.X, (int)balloon4.position.Y, balloon4.texture.Width - 20, balloon4.texture.Height - 51);
            this.balloon5.rectangle = new Rectangle((int)balloon5.position.X, (int)balloon5.position.Y, balloon5.texture.Width - 20, balloon5.texture.Height - 51);
            this.balloon6.rectangle = new Rectangle((int)balloon6.position.X, (int)balloon6.position.Y, balloon6.texture.Width - 20, balloon6.texture.Height - 51);

            this.popedBalloons = new bool[6];
            for (int i = 0; i < this.popedBalloons.Length; i++)
            {
                this.popedBalloons[i] = false;
            }


            //ROACH GAME 
            roach = new Sprite(content.Load<Texture2D>(@"Images\barata"), new Vector2(ScreenWidth / 2, ScreenHeight / 2));

            deadRoach = new Sprite(content.Load<Texture2D>(@"Images\barataMorta"), new Vector2(0, 0));
            shoe = new Sprite(content.Load<Texture2D>(@"Images\chinelo"), new Vector2(0, 0));
            bugBackground = content.Load<Texture2D>(@"Images\insetoBackground");

            roachColor = new Color[roach.texture.Width * roach.texture.Height];
            shoeColor = new Color[shoe.texture.Width * shoe.texture.Height];

            roach.texture.GetData(roachColor);
            shoe.texture.GetData(shoeColor);

            roach.rectangle = new Rectangle((int)roach.position.X, (int)roach.position.Y, roach.texture.Width, roach.texture.Height);
            shoe.rectangle = new Rectangle((int)shoe.position.X, (int)shoe.position.Y, shoe.texture.Width, shoe.texture.Height);

            roachIsDead = false;
            roachRotation = 45;
            lastDirection = -1;
            direction = -1;
            this.isToChangeDirection = true;

            //JAR MICROGAME
            hammer = new Sprite(content.Load<Texture2D>(@"Images\martelo2"), new Vector2(ScreenWidth / 2, ScreenHeight / 2));
            lightJar = new Sprite(content.Load<Texture2D>(@"Images\jarroLeve"), new Vector2(ScreenWidth / 2, 0));
            middleHeavyJar = new Sprite(content.Load<Texture2D>(@"Images\jarroMeioPesado"), new Vector2(ScreenWidth / 2, 0));
            heavyJar = new Sprite(content.Load<Texture2D>(@"Images\jarroPesado"), new Vector2(ScreenWidth / 2, 0));
            person = new Sprite(content.Load<Texture2D>(@"Images\pessoa2"), new Vector2(ScreenWidth / 2 - 50, ScreenHeight - 270));
            hurtPerson = new Sprite(content.Load<Texture2D>(@"Images\pessoaMachucada2"), new Vector2(ScreenWidth / 2 - 50, ScreenHeight - 270));
            jarBackground = content.Load<Texture2D>(@"Images\jarroBackground");

            lightJarColor = new Color[lightJar.texture.Width * lightJar.texture.Height];
            middleHeavyJarColor = new Color[middleHeavyJar.texture.Width * middleHeavyJar.texture.Height];
            heavyJarColor = new Color[heavyJar.texture.Width * heavyJar.texture.Height];
            personColor = new Color[person.texture.Width * person.texture.Height];
            hammerColor = new Color[hammer.texture.Width * hammer.texture.Height];

            lightJar.texture.GetData(lightJarColor);
            middleHeavyJar.texture.GetData(middleHeavyJarColor);
            heavyJar.texture.GetData(heavyJarColor);
            person.texture.GetData(personColor);
            hammer.texture.GetData(hammerColor);

            lightJar.rectangle = new Rectangle((int)lightJar.position.X, (int)lightJar.position.Y, lightJar.texture.Width, lightJar.texture.Height);
            middleHeavyJar.rectangle = new Rectangle((int)middleHeavyJar.position.X, (int)middleHeavyJar.position.Y, middleHeavyJar.texture.Width, middleHeavyJar.texture.Height);
            heavyJar.rectangle = new Rectangle((int)heavyJar.position.X, (int)heavyJar.position.Y, heavyJar.texture.Width, heavyJar.texture.Height);
            person.rectangle = new Rectangle((int)person.position.X, (int)person.position.Y, person.texture.Width, person.texture.Height);
            hammer.rectangle = new Rectangle((int)hammer.position.X, (int)hammer.position.Y, hammer.texture.Width, hammer.texture.Height);

            isPersonHurt = false;
            numberOfColisions = 0;

            //ALL MICROGAMES
            chances = content.Load<Texture2D>(@"Images\chances");
            chancesName = new Sprite(content.Load<Texture2D>(@"Images\chancesNome"), new Vector2(500, 550));

        }

        public override void UnloadContent()
        {

        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            switch (micro)
            {
                case MicrogamesType.BALLOONPOP:
                    spriteBatch.Draw(ballonBackground, new Vector2(0, 0), Color.White);
                    if (!this.popedBalloons[0]) { spriteBatch.Draw(balloon.texture, balloon.position, Color.White); }
                    if (!this.popedBalloons[1]) { spriteBatch.Draw(balloon2.texture, balloon2.position, Color.White); }
                    if (!this.popedBalloons[2]) { spriteBatch.Draw(balloon3.texture, balloon3.position, Color.White); }
                    if (!this.popedBalloons[3]) { spriteBatch.Draw(balloon4.texture, balloon4.position, Color.White); }
                    if (!this.popedBalloons[4]) { spriteBatch.Draw(balloon5.texture, balloon5.position, Color.White); }
                    if (!this.popedBalloons[5]) { spriteBatch.Draw(balloon6.texture, balloon6.position, Color.White); }
                    spriteBatch.Draw(needle.texture, needle.position, Color.White);
                    break;

                case MicrogamesType.BIKEWHEEL:
                    break;
                case MicrogamesType.BUG:
                    spriteBatch.Draw(bugBackground, new Vector2(0, 0), Color.White);
                    if (!roachIsDead)
                    {
                        spriteBatch.Draw(roach.texture, roach.position, null, Color.White, MathHelper.ToRadians(roachRotation), Vector2.Zero, Vector2.One, SpriteEffects.None, 0);
                    }
                    else
                    {
                        deadRoach.position = roach.position;
                        spriteBatch.Draw(deadRoach.texture, deadRoach.position, Color.White);
                    }
                    spriteBatch.Draw(shoe.texture, shoe.position, Color.White);
                    break;
                case MicrogamesType.BYE:
                    break;
                case MicrogamesType.FLOWERJAR:
                    spriteBatch.Draw(jarBackground, new Vector2(0, 0), Color.White);
                    if (!isPersonHurt)
                    {
                        spriteBatch.Draw(person.texture, person.position, Color.White);
                        if (numberOfColisions < 2)
                        {
                            spriteBatch.Draw(lightJar.texture, lightJar.position, Color.White);
                        }
                        else if (numberOfColisions < 4)
                        {
                            spriteBatch.Draw(middleHeavyJar.texture, middleHeavyJar.position, Color.White);
                        }
                        else
                        {
                            spriteBatch.Draw(heavyJar.texture, heavyJar.position, Color.White);
                        }
                    }
                    else
                    {
                        spriteBatch.Draw(hurtPerson.texture, hurtPerson.position, Color.White);
                    }
                    spriteBatch.Draw(hammer.texture, hammer.position, Color.White);
                    break;
                case MicrogamesType.FRUITCUT:
                    break;
                case MicrogamesType.NURSE:
                    break;
                case MicrogamesType.PULLEY:
                    break;
                case MicrogamesType.SAYTHEDIFERENT:
                    break;
                case MicrogamesType.SAYTHEWRONG:
                    break;
            }

            spriteBatch.Draw(chancesName.texture, chancesName.position, Color.White);


            switch (numberOfChances)
            {
                case 1:
                    spriteBatch.Draw(chances, new Vector2(660, 550), Color.White);
                    break;
                case 2:
                    spriteBatch.Draw(chances, new Vector2(660, 550), Color.White);
                    spriteBatch.Draw(chances, new Vector2(690, 550), Color.White);
                    break;
                case 3:
                    spriteBatch.Draw(chances, new Vector2(660, 550), Color.White);
                    spriteBatch.Draw(chances, new Vector2(690, 550), Color.White);
                    spriteBatch.Draw(chances, new Vector2(720, 550), Color.White);
                    break;

            }

            spriteBatch.End();
        }
        public override void Update(GameTime gameTime)
        {


            if (Input.KeyPressed(Keys.Escape))
            {
                FinishGame();
            }

            this.timeSpent += gameTime.ElapsedGameTime.TotalSeconds;
            switch (micro)
            {
                case MicrogamesType.BALLOONPOP:
                    if (timeSpent > 3)
                    {
                        bool stop = false;
                        for (int i = 0; i < 6 && !stop; i++)
                        {
                            if (!this.popedBalloons[i])
                            {
                                this.numberOfChances -= 1;
                                stop = true;
                            }
                        }
                        if (this.numberOfChances > 0)
                        {
                            this.micro = MicrogamesType.BUG; //mudar de jogo
                            this.timeSpent = 0;
                        }
                        else
                        {
                            //TODO: tela de gameover
                        }
                    }
                    else
                    {
                        this.moveCursor();
                    }
                    break;

                case MicrogamesType.BIKEWHEEL:
                    break;
                case MicrogamesType.BUG:
                    if (!roachIsDead) { this.moveObject(); }
                    if (timeSpent > 5)
                    {
                        if (!roachIsDead)
                        {
                            this.numberOfChances -= 1;
                        }

                        if (this.numberOfChances > 0)
                        {
                            this.micro = MicrogamesType.FLOWERJAR; //mudar de jogo
                            this.timeSpent = 0;
                        }
                        else
                        {
                            //TODO: tela de gameover
                        }
                    }
                    else
                    {
                        this.moveCursor();
                    }
                    break;
                case MicrogamesType.BYE:
                    break;
                case MicrogamesType.FLOWERJAR:
                    if (!isPersonHurt)
                    {
                        this.moveCursor();
                        this.moveObject();
                        if (detectColision(person, lightJar, personColor, lightJarColor)
                            || detectColision(person, middleHeavyJar, personColor, middleHeavyJarColor)
                            || detectColision(person, heavyJar, personColor, heavyJarColor))
                        {
                            isPersonHurt = true;
                        }
                        else
                        {
                            if (numberOfColisions == 7)
                            {
                                this.micro = MicrogamesType.BALLOONPOP;
                            }
                        }
                    }
                    else
                    {
                        if (this.numberOfChances > 0)
                        {
                            this.micro = MicrogamesType.BALLOONPOP; //mudar de jogo
                        }
                        else
                        {
                            //TODO: tela de gameover
                        }
                    }
                    break;
                case MicrogamesType.FRUITCUT:
                    break;
                case MicrogamesType.NURSE:
                    break;
                case MicrogamesType.PULLEY:
                    break;
                case MicrogamesType.SAYTHEDIFERENT:
                    break;
                case MicrogamesType.SAYTHEWRONG:
                    break;
            }

        }


        private void moveCursor()
        {
            MouseState mouse = Mouse.GetState();

            switch (micro)
            {
                case MicrogamesType.BALLOONPOP:

                    this.needle.position.X = mouse.X;
                    this.needle.position.Y = mouse.Y;
                    this.refreshRectangles();

                    if (!this.wasPressed && mouse.LeftButton == ButtonState.Pressed)
                    {
                        if (!this.popedBalloons[0] && this.detectColision(this.needle, this.balloon, this.needleColor, this.balloonColor))
                        {
                            this.popedBalloons[0] = true;
                            this.wasPressed = true;
                        }
                        else if (!this.popedBalloons[1] && this.detectColision(this.needle, this.balloon2, this.needleColor, this.balloon2Color))
                        {
                            this.popedBalloons[1] = true;
                            this.wasPressed = true;
                        }
                        else if (!this.popedBalloons[2] && this.detectColision(this.needle, this.balloon3, this.needleColor, this.balloon3Color))
                        {
                            this.popedBalloons[2] = true;
                            this.wasPressed = true;
                        }
                        else if (!this.popedBalloons[3] && this.detectColision(this.needle, this.balloon4, this.needleColor, this.balloon4Color))
                        {
                            this.popedBalloons[3] = true;
                            this.wasPressed = true;
                        }
                        else if (!this.popedBalloons[4] && this.detectColision(this.needle, this.balloon5, this.needleColor, this.balloon5Color))
                        {
                            this.popedBalloons[4] = true;
                            this.wasPressed = true;
                        }
                        else if (!this.popedBalloons[5] && this.detectColision(this.needle, this.balloon6, this.needleColor, this.balloon6Color))
                        {
                            this.popedBalloons[5] = true;
                            this.wasPressed = true;
                        }
                    }
                    else if (this.wasPressed && mouse.LeftButton == ButtonState.Released)
                    {
                        this.wasPressed = false;
                    }

                    break;

                case MicrogamesType.BIKEWHEEL:
                    break;
                case MicrogamesType.BUG:
                    this.shoe.position.X = mouse.X;
                    this.shoe.position.Y = mouse.Y;
                    this.refreshRectangles();

                    if (!this.wasPressed && mouse.LeftButton == ButtonState.Pressed && !roachIsDead && detectColision(this.shoe, this.roach, this.shoeColor, this.roachColor))
                    {
                        roachIsDead = true;
                        wasPressed = true;
                    }
                    else if (this.wasPressed && mouse.LeftButton == ButtonState.Released)
                    {
                        this.wasPressed = false;
                    }
                    break;
                case MicrogamesType.BYE:
                    break;
                case MicrogamesType.FLOWERJAR:
                    this.hammer.position.X = mouse.X;
                    this.hammer.position.Y = mouse.Y;
                    if (!this.wasPressed && mouse.LeftButton == ButtonState.Pressed && numberOfColisions < 7)
                    {
                        if (detectColision(hammer, lightJar, hammerColor, lightJarColor)
                            || detectColision(hammer, middleHeavyJar, hammerColor, middleHeavyJarColor)
                            || detectColision(hammer, heavyJar, hammerColor, heavyJarColor))
                        {
                            numberOfColisions++;
                        }
                    }
                    break;
                case MicrogamesType.FRUITCUT:
                    break;
                case MicrogamesType.NURSE:
                    break;
                case MicrogamesType.PULLEY:
                    break;
                case MicrogamesType.SAYTHEDIFERENT:
                    break;
                case MicrogamesType.SAYTHEWRONG:
                    break;
            }
        }

        private void refreshRectangles()
        {
            switch (micro)
            {
                case MicrogamesType.BALLOONPOP:
                    this.needle.rectangle = new Rectangle((int)needle.position.X, (int)needle.position.Y, needle.texture.Width - 25, needle.texture.Height - 20);
                    break;

                case MicrogamesType.BIKEWHEEL:
                    break;
                case MicrogamesType.BUG:
                    this.roach.rectangle = new Rectangle((int)roach.position.X, (int)roach.position.Y, roach.texture.Width, roach.texture.Height);
                    this.shoe.rectangle = new Rectangle((int)shoe.position.X, (int)shoe.position.Y, shoe.texture.Width, shoe.texture.Height);
                    break;
                case MicrogamesType.BYE:
                    break;
                case MicrogamesType.FLOWERJAR:
                    this.hammer.rectangle = new Rectangle((int)hammer.position.X, (int)hammer.position.Y, hammer.texture.Width, hammer.texture.Height);    
                    this.lightJar.rectangle = new Rectangle((int)lightJar.position.X, (int)lightJar.position.Y, lightJar.texture.Width, lightJar.texture.Height);
                    this.heavyJar.rectangle = new Rectangle((int)heavyJar.position.X, (int)heavyJar.position.Y, heavyJar.texture.Width, heavyJar.texture.Height);
                    this.middleHeavyJar.rectangle = new Rectangle((int)middleHeavyJar.position.X, (int)middleHeavyJar.position.Y, middleHeavyJar.texture.Width, middleHeavyJar.texture.Height);
                break;
                case MicrogamesType.FRUITCUT:
                    break;
                case MicrogamesType.NURSE:
                    break;
                case MicrogamesType.PULLEY:
                    break;
                case MicrogamesType.SAYTHEDIFERENT:
                    break;
                case MicrogamesType.SAYTHEWRONG:
                    break;
            }
        }

        private bool detectColision(Sprite spriteCursor, Sprite spriteObject, Color[] colorSpriteCursor, Color[] colorSpriteObject)
        {

            float x1 = spriteCursor.position.X;
            float x2 = spriteCursor.position.X + spriteCursor.rectangle.Width;
            float x3 = spriteObject.position.X;
            float x4 = spriteObject.position.X + spriteObject.rectangle.Width;
            float y1 = spriteCursor.position.Y;
            float y2 = spriteCursor.position.Y + spriteCursor.rectangle.Height;
            float y3 = spriteObject.position.Y;
            float y4 = spriteObject.position.Y + spriteObject.rectangle.Height;

            //teste os limites e veja se os objetos colidiram

            bool colided = false;

            if ((((x3 <= x1) && (x1 <= x4)) || ((x3 <= x2) && (x2 <= x4))) && (((y3 <= y1) && (y1 <= y4)) || ((y3 <= y2) && (y2 <= y4))))
            {
                //encontrando as coordenadas da interseção das imagens

                int top = Math.Max(spriteCursor.rectangle.Top, spriteObject.rectangle.Top);
                int bottom = Math.Min(spriteCursor.rectangle.Bottom, spriteObject.rectangle.Bottom);
                int left = Math.Max(spriteCursor.rectangle.Left, spriteObject.rectangle.Left);
                int right = Math.Min(spriteCursor.rectangle.Right, spriteCursor.rectangle.Right);

                for (int y = top; y < bottom; y++)
                {

                    //enquanto existirem colunas na interseção,
                    //continue procurando pontos
                    for (int x = left; x < right; x++)
                    {
                        //o array de cores só tem uma dimensão, calcule as
                        //coordenadas e retorne a cor
                        //daquela posição

                        Color colorA = colorSpriteCursor[(y - spriteCursor.rectangle.Top) * spriteCursor.rectangle.Width + (x - spriteCursor.rectangle.Left)];

                        //ache as coordenadas para o outro objeto

                        Color colorB = colorSpriteObject[(y - spriteObject.rectangle.Top) * spriteObject.rectangle.Width + (x - spriteObject.rectangle.Left)];

                        //agora que já achou as cores, teste se alguma delas é
                        //transparente
                        //se nenhuma das cores for transparente, colidiu!

                        if (colorA.A != 0 && colorB.A != 0)
                        {
                            //encontrou colisão
                            return true;
                        }
                    }

                }
            }
            return colided;
        }

        private void Finalize(object sender, EventArgs e)
        {
            this.GameCancelled(0);
        }

        private void FinishGame()
        {
            this.Active = false;
        }

        private void moveObject()
        {
            switch (micro)
            {
                case MicrogamesType.BUG:

                    if (((roach.position.X + roach.texture.Width) < roach.texture.Width + roach.texture.Width / 2)
                        || ((roach.position.X + roach.texture.Width) > ScreenWidth)
                        || ((roach.position.Y + roach.texture.Height) < roach.texture.Height + roach.texture.Height / 2)
                        || ((roach.position.Y + roach.texture.Height) > ScreenHeight))
                    {
                        isToChangeDirection = true;
                    }

                    if (isToChangeDirection)
                    {
                        Random random = new Random();
                        while (direction == lastDirection)
                        {
                            direction = random.Next(0, 8);
                        }
                        lastDirection = direction;
                        isToChangeDirection = false;
                    }

                    switch (direction)
                    {
                        case 0: //Norte
                            roach.position.Y -= 10;
                            break;
                        case 1: //Nordeste
                            roachRotation = 45;
                            roach.position.Y -= 10;
                            roach.position.X += 10;
                            break;
                        case 2: //Leste
                            roachRotation = 90;
                            roach.position.X += 10;
                            break;
                        case 3: //Sudeste
                            roachRotation = 135;
                            roach.position.X += 10;
                            roach.position.Y += 10;
                            break;
                        case 4: //Sul
                            roachRotation = 180;
                            roach.position.Y += 10;
                            break;
                        case 5: //Sudoeste
                            roachRotation = 225;
                            roach.position.X -= 10;
                            roach.position.Y += 10;
                            break;
                        case 6: //Oeste
                            roachRotation = 270;
                            roach.position.X -= 10;
                            break;
                        case 7: //Noroeste
                            roachRotation = 315;
                            roach.position.X -= 10;
                            roach.position.Y -= 10;
                            break;

                    }



                    break;
                    case MicrogamesType.FLOWERJAR:
                        if(numberOfColisions < 2){
                            this.lightJar.position.Y += 5;
                        }
                        else if(numberOfColisions < 4){
                            this.middleHeavyJar.position.Y += 5;
                        }else if(numberOfColisions < 7){
                            this.heavyJar.position.Y += 5;
                        }
                    break;
            }
            //TODO outros casos em que a movimentacao na tela seja necessaria
        }

    }
}
